home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / alloca.c next >
Encoding:
C/C++ Source or Header  |  1993-02-24  |  6.2 KB  |  224 lines  |  [TEXT/MPS ]

  1. /*
  2.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  3.  
  4.     last edit:    86/05/30    rms
  5.        include config.h, since on VMS it renames some symbols.
  6.        Use xmalloc instead of malloc.
  7.    MPW C changes
  8.      Copyright (C) 1989, 1990 Apple Computer, Inc.
  9.  
  10.     This implementation of the PWB library alloca() function,
  11.     which is used to allocate space off the run-time stack so
  12.     that it is automatically reclaimed upon procedure exit, 
  13.     was inspired by discussions with J. Q. Johnson of Cornell.
  14.  
  15.     It should work under any C implementation that uses an
  16.     actual procedure stack (as opposed to a linked list of
  17.     frames).  There are some preprocessor constants that can
  18.     be defined when compiling for your specific system, for
  19.     improved efficiency; however, the defaults should be okay.
  20.  
  21.     The general concept of this implementation is to keep
  22.     track of all alloca()-allocated blocks, and reclaim any
  23.     that are found to be deeper in the stack than the current
  24.     invocation.  This heuristic does not reclaim storage as
  25.     soon as it becomes invalid, but it will do so eventually.
  26.  
  27.     As a special case, alloca(0) reclaims storage without
  28.     allocating any.  It is a good idea to use alloca(0) in
  29.     your main control loop, etc. to force garbage collection.
  30. */
  31. #ifndef lint
  32. static char    SCCSid[] = "@(#)alloca.c    1.1";    /* for the "what" utility */
  33. #endif
  34.  
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38.  
  39. #ifdef emacs
  40. #ifdef static
  41. /* actually, only want this if static is defined as ""
  42.    -- this is for usg, in which emacs must undefine static
  43.    in order to make unexec workable
  44.    */
  45. #ifndef STACK_DIRECTION
  46. you
  47. lose
  48. -- must know STACK_DIRECTION at compile-time
  49. #endif /* STACK_DIRECTION undefined */
  50. #endif /* static */
  51. #endif /* emacs */
  52.  
  53. #ifndef alloca  /* If compiling with GCC, this file's not needed.  */
  54.  
  55. #ifdef __STDC__
  56. typedef void    *pointer;        /* generic pointer type */
  57. #else
  58. typedef char    *pointer;        /* generic pointer type */
  59. #endif
  60.  
  61. #define    NULL    0            /* null pointer constant */
  62.  
  63. extern void    free();
  64. extern pointer    xmalloc();
  65.  
  66. /*
  67.     Define STACK_DIRECTION if you know the direction of stack
  68.     growth for your system; otherwise it will be automatically
  69.     deduced at run-time.
  70.  
  71.     STACK_DIRECTION > 0 => grows toward higher addresses
  72.     STACK_DIRECTION < 0 => grows toward lower addresses
  73.     STACK_DIRECTION = 0 => direction of growth unknown
  74. */
  75.  
  76. #ifndef STACK_DIRECTION
  77. #define    STACK_DIRECTION    0        /* direction unknown */
  78. #endif
  79.  
  80. #if STACK_DIRECTION != 0
  81.  
  82. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  83.  
  84. #else    /* STACK_DIRECTION == 0; need run-time code */
  85.  
  86. static int    stack_dir;        /* 1 or -1 once known */
  87. #define    STACK_DIR    stack_dir
  88.  
  89. static void
  90. find_stack_direction (/* void */)
  91. {
  92.   static char    *addr = NULL;    /* address of first
  93.                    `dummy', once known */
  94.   auto char    dummy;        /* to get stack address */
  95.  
  96.   if (addr == NULL)
  97.     {                /* initial entry */
  98.       addr = &dummy;
  99.  
  100.       find_stack_direction ();    /* recurse once */
  101.     }
  102.   else                /* second entry */
  103.     if (&dummy > addr)
  104.       stack_dir = 1;        /* stack grew upward */
  105.     else
  106.       stack_dir = -1;        /* stack grew downward */
  107. }
  108.  
  109. #endif    /* STACK_DIRECTION == 0 */
  110.  
  111. /*
  112.     An "alloca header" is used to:
  113.     (a) chain together all alloca()ed blocks;
  114.     (b) keep track of stack depth.
  115.  
  116.     It is very important that sizeof(header) agree with malloc()
  117.     alignment chunk size.  The following default should work okay.
  118. */
  119.  
  120. #ifndef    ALIGN_SIZE
  121. #define    ALIGN_SIZE    sizeof(double)
  122. #endif
  123.  
  124. typedef union hdr
  125. {
  126.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  127.   struct
  128.     {
  129.       union hdr *next;        /* for chaining headers */
  130.       char *deep;        /* for stack depth measure */
  131.     } h;
  132. } header;
  133.  
  134. /*
  135.     alloca( size ) returns a pointer to at least `size' bytes of
  136.     storage which will be automatically reclaimed upon exit from
  137.     the procedure that called alloca().  Originally, this space
  138.     was supposed to be taken from the current stack frame of the
  139.     caller, but that method cannot be made to work for some
  140.     implementations of C, for example under Gould's UTX/32.
  141. */
  142.  
  143. static header *last_alloca_header = NULL; /* -> last alloca header */
  144. int totalloca = 0;
  145. #ifdef MPW_C
  146. /* MPW C plays games with the stack pointer, in much the same way that GCC
  147.    does, but unfortunately it can't be told to be careful around alloca.
  148.    So instead of looking at sp, we look at the frame pointer of alloca's
  149.    caller, which is known to always be defined and not to change within
  150.    its scope of definition.  Doing this requires a direct function that
  151.    picks up the caller's a6, using the single instruction "move.l (a6),d0". */
  152.      
  153. static char *frame_pointer_value() = 0x2016;
  154.  
  155. /* This variable just keeps track of much alloca is used. */
  156.  
  157. #endif /* MPW_C */
  158.  
  159.  
  160. pointer
  161. alloca (size)            /* returns pointer to storage */
  162.      unsigned    size;        /* # bytes to allocate */
  163. {
  164. #ifdef MPW_C
  165.   /* Use the caller's frame pointer to estimate depth in the stack. */
  166.   register char *depth = frame_pointer_value();
  167. #else
  168.   auto char    probe;        /* probes stack depth: */
  169.   register char    *depth = &probe;
  170. #endif /* MPW_C */
  171. #ifdef MPW_C
  172.   /* Count this call against the total allocation. */
  173.   totalloca += size;
  174. #endif /* MPW_C */
  175.  
  176. #if STACK_DIRECTION == 0
  177.   if (STACK_DIR == 0)        /* unknown growth direction */
  178.     find_stack_direction ();
  179. #endif
  180.  
  181.                 /* Reclaim garbage, defined as all alloca()ed storage that
  182.                    was allocated from deeper in the stack than currently. */
  183.  
  184.   {
  185.     register header    *hp;    /* traverses linked list */
  186.  
  187.     for (hp = last_alloca_header; hp != NULL;)
  188.       if ((STACK_DIR > 0 && hp->h.deep > depth)
  189.       || (STACK_DIR < 0 && hp->h.deep < depth))
  190.     {
  191.       register header    *np = hp->h.next;
  192.  
  193.       free ((pointer) hp);    /* collect garbage */
  194.  
  195.       hp = np;        /* -> next header */
  196.     }
  197.       else
  198.     break;            /* rest are not deeper */
  199.  
  200.     last_alloca_header = hp;    /* -> last valid storage */
  201.   }
  202.  
  203.   if (size == 0)
  204.     return NULL;        /* no allocation required */
  205.  
  206.   /* Allocate combined header + user data storage. */
  207.  
  208.   {
  209.     register pointer    new = xmalloc (sizeof (header) + size);
  210.     /* address of header */
  211.  
  212.     ((header *)new)->h.next = last_alloca_header;
  213.     ((header *)new)->h.deep = depth;
  214.  
  215.     last_alloca_header = (header *)new;
  216.  
  217.     /* User storage begins just after header. */
  218.  
  219.     return (pointer)((char *)new + sizeof(header));
  220.   }
  221. }
  222.  
  223. #endif /* no alloca */
  224.